package system

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	uuid "github.com/satori/go.uuid"
	"new_club_recruitment/global"
	"new_club_recruitment/model/admin"
	common "new_club_recruitment/model/common/request"
	"new_club_recruitment/model/system"
	"new_club_recruitment/utils"
	"strconv"
	"time"
)

type RedisServe struct{}

// Puttoken 将token放入MySQL和redis
func (RedisServe *RedisServe) Puttoken(key, token string) (err error) {
	db := global.GVA_DB
	rdb := global.GVA_REDIS
	err = db.Model(admin.SysAdmin{}).Where(fmt.Sprintf("id = %s", key)).Update("token", token).Error
	if err != nil {
		return
	}
	err = rdb.Set(context.Background(), key, token, time.Hour).Err()
	if err != nil {
		return
	}
	return nil
}

// CheckToken 查找比对token
func (RedisServe *RedisServe) CheckToken(key, token string) (ok bool) {
	rdb := global.GVA_REDIS
	res, err := rdb.Get(context.Background(), key).Result()
	if err != nil {
		return false
	}
	if res != token {
		return false
	}
	err = rdb.Set(context.Background(), key, token, time.Hour).Err()
	if err != nil {
		return false
	}
	return true
}

// CreateRedEnvelope 创建红包
func (RedisServe *RedisServe) CreateRedEnvelope(RedEnvelope *common.RedEnvelopeInfo) (err error) {
	rdb := global.GVA_REDIS
	err = rdb.LPush(context.Background(), "RedEnvelope:"+RedEnvelope.Id.String(), nil).Err()
	var arr []interface{}
	remain := RedEnvelope.AllMoney
	for i := int64(0); i < RedEnvelope.Num; i++ {
		x := utils.DoubleAverage(RedEnvelope.Num-i, remain)
		remain -= x
		y := float64(x) / float64(100)
		arr = append(arr, strconv.FormatFloat(y, 'f', 2, 64))
	}
	RedEnvelope.Id = uuid.NewV4()
	err = rdb.LPush(context.Background(), "RedEnvelope:"+RedEnvelope.Id.String(), arr).Err()
	if err != nil {
		return err
	}
	err = rdb.HSet(context.Background(), "UserId:"+RedEnvelope.Id.String(), "AllMoney", RedEnvelope.AllMoney).Err()
	if err != nil {
		return err
	}
	err = rdb.Expire(context.Background(), "RedEnvelope:"+RedEnvelope.Id.String(), RedEnvelope.Time).Err()
	if err != nil {
		return err
	}
	err = rdb.Expire(context.Background(), "UserId:"+RedEnvelope.Id.String(), RedEnvelope.Time).Err()
	if err != nil {
		return err
	}
	return
}

// GetRedEnvelope 抢红包
func (RedisServe *RedisServe) GetRedEnvelope(user *common.RedEnvelopeGrabber) (res string, err error) {
	rdb := global.GVA_REDIS
	db := global.GVA_DB
	_, err = rdb.HGet(context.Background(), "UserId:"+user.Key, user.UserId).Result()
	if err == redis.Nil {
		res, err = rdb.LPop(context.Background(), "RedEnvelope:"+user.Key).Result()
		fmt.Println("RedEnvelope:" + user.Key)
		if err != nil {
			return "", err
		}
		err = rdb.HSet(context.Background(), "UserId:"+user.Key, user.UserId, res).Err()
		if err != nil {
			return "", err
		}
		//写进数据库中
		err = db.AutoMigrate(&system.RedEnvelope{})
		if err != nil {
			return "", err
		}
		db.Create(&system.RedEnvelope{UserId: user.UserId, Money: res})
		return
	} else {
		return "", fmt.Errorf("重复抢红包！")
	}

}

// RedEnvelopeRes 抢红包结果
func (RedisServe *RedisServe) RedEnvelopeRes(key string) (res map[string]string, err error) {
	rdb := global.GVA_REDIS
	res, err = rdb.HGetAll(context.Background(), "UserId:"+key).Result()
	fmt.Println(res)
	if err != nil {
		return nil, err
	}
	return
}
